
<script type="text/javascript">
    //树表格
    $(function () {
        $('#list').datagrid({
            url          : '<?=site_url("admin/admin/getAdminList");?>?d=d',
            idField      : 'id',
            fitColumns   : true,
            //treeField: 'title',
            collapseAll  : true,
            singleSelect : true,
            fit          : true,
            doSize       : true,
            closable     : true,
            striped      : true,
            rownumbers   : true,
            toolbar      : $("#toolbal"),
            onDblClickRow: function () {
                showDialog("修改");
            },
            checkbox     : true,
            frozenColumns: [[
                {field: 'id', title: 'ID'},
            ]],
            columns      : [[
                {fiele: 'info', title: '基本信息', colspan: 9, align: 'center'},
                {
                    field    : 'option',
                    title    : '操作',
                    rowspan  : 2,
                    align    : 'center',
                    width    : 80,
                    formatter: function (value, row, index) {
                        if(row.id==1)
                        {
                            return "<span onclick='addToGroup(" + row.id + ")'>添加到组</span> | <span onclick='edit(" + row.id + ")' >编辑</span>";
                        }else{
                            return "<span onclick='addToGroup(" + row.id + ")'>添加到组</span> | <span onclick='edit(" + row.id + ")' >编辑</span> | <span onclick='del(" + row.id + ")'>删除</span>";
                        }

                    }
                }
            ], [
                {field: 'name', title: '名称'},
                {field: 'email', title: '邮箱'},
                {field: 'mobile', title: '手机'},
                {field: 'reg_time', title: '注册时间'},
                {field: 'reg_ip', title: '注册IP'},
                {field: 'last_login_time', title: '最后登录时间'},
                {field:'last_login_ip', title: '最后登录IP'},
                {field:'login_times', title: '登录次数'},
                {
                    field: 'status',
                    title: '状态',
                    formatter: function (value, row, index) {
                        if (value == 1)return "<span style='color:darkgreen;'>可用</span>";
                        else return "<span style='color:red;'>禁用</span>";
                    }
                },
            ]]
        });
    })

    //对话框
    function showDialog(option) {
        $("#form").dialog({
            title  : option + '数据',
            closed : false,
            cache  : false,
            modal  : true,
            buttons: [{
                text   : '保存',
                iconCls: 'icon-ok',
                handler: function () {
                    saveData();
                }
            }, {
                text   : '关闭',
                iconCls: 'icon-cancel',
                handler: function () {
                    $("#form").dialog('close');
                }
            }]
        });
        $("#form").form("clear");
        if (option == "修改") {
            var node = $('#list').datagrid('getSelected');
            $('#form').form('load', node);
        }
    }
    //删除
    function del(obj) {
        $.messager.confirm("删除管理员", "你确定删除此管理员?", function (r) {
            if (!r) return;
            $.post(
                "<?=site_url('admin/admin/delete');?>",
                {id:obj},
                function (data) {
                    if(data.isok)
                    {
                        $.messager.show({msg:'操作成功!', style:{}});
                        $("#list").datagrid('reload');
                    }
                    else
                    {
                        $.messager.show({msg:data.error, style:{}});
                    }
                },
                'json'
            );
        });
    }
    //保存
    function saveData() {
        $("#form").form('submit', {
            url     : "<?=site_url('admin/admin/save');?>",
            onSubmit: function () {
                var isValid = $(this).form('validate');
                if (!isValid) {
                    $.messager.show({msg: '请填写必填项'});
                }
                return isValid;
            },
            success : function (data) {
                data = eval("(" + data + ")");
                if (data.isok) {
                    $.messager.show({msg:'操作成功!', style:{}});
                    $("#list").datagrid("reload");
                } else {
                    $.messager.show({msg:data.error, style:{}});
                }
                $("#form").dialog("close");
            }
        })
    }

    function edit(obj) {
        $("#list").datagrid('selectRecord', obj);
        showDialog("修改");
    }
    function memberAcl(obj) {
        alert(obj)
    }
    function addToGroup(obj) {
        $("#list").datagrid('selectRecord', obj);
        var node = $('#list').datagrid('getSelected');
        $("#tree").dialog({
            title    : '添加['+node.name+']到组',
            resizable: true,
            doSize   : true,
            fit      : true,
            buttons  : [{
                text   : '保存',
                iconCls: 'icon-ok',
                handler: function () {
                    saveGroupAcess(obj);
                    $("#tree").dialog('close');
                }
            }, {
                text   : '关闭',
                iconCls: 'icon-cancel',
                handler: function () {
                    $("#tree").dialog('close');
                }
            }]
        }).tree({
            url         : '<?=site_url("admin/admin/getGroupList");?>?id=' + obj,
            checkbox     : true,
            formatter : function (node) {
                node.text="["+node.text+']组';
                return node.text;
            },
            onLoadSuccess:function(node,data)
            {
                if(!data.isok)
                {
                    $.messager.show({msg:data.error, style:{}});
                    $("#tree").dialog('close');
                }
            }
        })
    }
    function saveGroupAcess(obj) {
        var nodes = $('#tree').tree('getChecked');
        var nodes_ids = [];
        for (var i = 0; i < nodes.length; i++) {
            nodes_ids[i] = nodes[i].id;
        }
        $.post(
            '<?=site_url("admin/admin/saveGroupAcess");?>',
            {id:obj,group:nodes_ids.toString()},
            function(data)
            {
                if(!data.isok)
                {
                    $.messager.show({msg:data.error, style:{}});
                }else{
                    $.messager.show({msg:'操作成功!', style:{}});
                    $("#list").datagrid('reload');
                }
            },
            'json'
        );
    }

</script>
<div id="toolbal" style="padding: 5px;">
    <span onclick="showDialog('添加')" class="easyui-linkbutton" data-options="iconCls:'icon-add'">添加</span>
    <form style="padding:5px;display: inline;">
         时间: <input class="easyui-datebox j_datebox" name="starttime" value="">
         文章: <input class="easyui-validatebox textbox" type="text" name="orderinfo_goodssn" value="">
         类型: <input class="easyui-validatebox textbox" type="text" name="orderinfo_orderinfosn" value="">
         <span id="j_finish_so" href="#" class="easyui-linkbutton" iconCls="icon-search">条件搜索</span>
         <span id="j_finish_clear" href="#" class="easyui-linkbutton" iconCls="icon-search">清空条件</span>
     </form>
     <span style="margin-right: 10px;" onclick="window.location.reload();" class="easyui-linkbutton" data-options="iconCls:'icon-reload'">刷新本页面</span>

</div>
<ul id="list"></ul>

<form id="form" closed="true">
    <input name="id" type="hidden">
    <table>
        <tr>
            <td align="right">名　　称：</td>
            <td><input class="easyui-textbox" name="name" required="true"/></td>
        </tr>
        <tr>
            <td align="right">密　　码：</td>
            <td><input type="password" class="easyui-textbox" name="password"/></td>
        </tr>
        <tr>
            <td align="right">手　　机：</td>
            <td><input name="mobile" class="easyui-textbox"/></td>
            <td></td>
        </tr>
        <tr>
            <td align="right">邮　　箱：</td>
            <td><input name="email" class="easyui-textbox"/></td>
            <td></td>
        </tr>
        <tr>
            <td align="right">状　　态：</td>
            <td>
                <select name="status" class="easyui-combobox" required="true">
                    <option value="0">禁用</option>
                    <option value='1'>可用</option>
                </select>
            </td>
            <td></td>
        </tr>

    </table>

</form>
<ul id="tree"></ul>

